import {readFileSync, writeFileSync} from 'node:fs'
import {BrowserWindow, app} from "electron";
import {join} from "node:path";

// 存储窗口配置的路径
const windowStateFile = 'window-state.json';

/**
 * 保存窗口大小
 * @param win
 */
export const saveWindowState = (win: BrowserWindow) => {
  const {width, height} = win.getBounds();
  const state = {width, height};
  writeFileSync(join(app.getPath('userData'), windowStateFile), JSON.stringify(state));
}

/**
 * 获取窗口大小
 */
export const getStateFile = () => {
  // 加载之前保存的窗口状态
  let state = {width: 630, height: 400};
  try {
    const data = readFileSync(join(app.getPath('userData'), windowStateFile));
    if (data) state = JSON.parse(data.toString());
  } catch (err) {
    return state
    // 如果读取失败，忽略错误并使用默认值
  }
  return state
}
